// zofui_sales/sort/components/cart-list.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    cart: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    datalist: [],
    Time: app.globalData.Time,
    mylike: [],
    siteroot: app.siteInfo.siteroot.replace('app/index.php', ''),
    uniacid: app.siteInfo.uniacid,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clickOverlay() {
      this.triggerEvent('closecartlist')
    },
    // 阻止滑动事件
    moveHandle() {},
    // 购物车加减
    miusVal(e) {
      var self = this;
      var id = e.currentTarget.dataset.id;
      var type = e.currentTarget.dataset.type;
      var num = 1;
      for (var i = 0; i < self.data.cart.goodlist.length; i++) {
        var item = self.data.cart.goodlist[i].list;
        for (var j = 0; j < item.length; j++) {
          if (item[j].cid == id) {

            if (type == 1) {
              num = item[j].num * 1 - 1;
              if (num <= 0) {
                num = 0;
                self.del(id)
                return
              }
            } else if (type == 2) {
              num = item[j].num * 1 + 1;
              if (item[j].num >= item[j].stock) {
                num = item[j].stock;
              }
              console.log(item[j].num, item[j].stock);
            } else if (type == 3) {
              num = e.detail.value;
              if (num >= item[j].stock) {
                num = item[j].stock;
              }
            }

            self.setData({
              ['cart.goodlist[' + i + ']list[' + j + '].num']: num * 1
            });

            var postdata = {
              op: 'addnum',
              cid: id,
              num: num,
            }
            app.com.http('cart', 'POST', postdata, 0, false, '', '', function (res) {});
            break;
          }
        }
      }
      this.triggerEvent('getCartShop')
    },
    // 清空购物车
    clearCart(){
      this.data.cart.goodlist[0].list.forEach(item=> {
        this.del(item.cid)
      })
    },
    // 删除商品
    del(id) {
      var self = this;
      var postdata = {
        op: 'delete',
        id: id
      }
      app.com.http('cart', 'POST', postdata, 0, true, '', '', function (res) {
        app.com.toast(res.data.message);
        self.triggerEvent('getCartShop')
      });
    },
  },
  attached() {

  }
})